import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GUI {
	GameData gameData;
	JFrame f;
	JLabel[][] b;
	JButton upButton, downButton, leftButton, rightButton;
	GameControl gameControl;

	GUI(GameData gameData, GameControl gameControl) {
		this.gameData = gameData;
		this.gameControl = gameControl;
		f = new JFrame("Magic Tower");
		b = new JLabel[gameData.H][gameData.W];
		
		// 创建游戏地图面板
		JPanel mapPanel = new JPanel(new GridLayout(gameData.H, gameData.W));
		for (int i = 0; i < gameData.H; i++) {
			for (int j = 0; j < gameData.W; j++) {
				b[i][j] = new JLabel();
				mapPanel.add(b[i][j]);
			}
		}
		
		// 创建控制按钮面板
		JPanel controlPanel = new JPanel(new GridLayout(2, 3));
		// 创建方向按钮
		upButton = new JButton("上");
		downButton = new JButton("下");
		leftButton = new JButton("左");
		rightButton = new JButton("右");

		// 按照2x3的网格布局添加按钮和占位符
		controlPanel.add(new JLabel()); // 左上角空白标签作为占位符
		controlPanel.add(upButton);     // 上按钮位于中上位置
		controlPanel.add(new JLabel()); // 右上角空白标签作为占位符
		controlPanel.add(leftButton);   // 左按钮位于左下位置
		controlPanel.add(downButton);   // 下按钮位于中下位置
		controlPanel.add(rightButton);  // 右按钮位于右下位置

		// 为所有按钮设置动作监听器
		setButtonListeners();

		// 设置主窗口布局
		f.setLayout(new BorderLayout());
		// 将地图面板添加到窗口的中央
		f.add(mapPanel, BorderLayout.CENTER);
		// 将控制按钮面板添加到窗口的底部
		f.add(controlPanel, BorderLayout.SOUTH);

		// 设置窗口大小
		// 宽度 = 游戏宽度 * 每个格子的宽度 + 额外边距
		// 高度 = 游戏高度 * 每个格子的高度 + 控制面板高度 + 额外边距
		f.setSize(gameData.W * 100 + 10, gameData.H * 100 + 150);
		// 使窗口可见
		f.setVisible(true);
		// 设置关闭窗口时的操作为退出程序
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 刷新GUI，显示初始游戏状态
		refreshGUI();
	}

	private void setButtonListeners() {
		// 为上移按钮添加动作监听器
		upButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// 当按钮被点击时，调用游戏控制器的handleInput方法，传入'w'表示向上移动
				gameControl.handleInput('w');
				// 刷新GUI以显示移动后的游戏状态
				refreshGUI();
			}
		});

		downButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				gameControl.handleInput('s');
				refreshGUI();
			}
		});

		leftButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				gameControl.handleInput('a');
				refreshGUI();
			}
		});

		rightButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				gameControl.handleInput('d');
				refreshGUI();
			}
		});
	}

	public void refreshGUI()
	{
		for (int i = 0; i < gameData.H; i++) {
			for (int j = 0; j < gameData.W; j++) {
				Image scaledImage = chooseImage(gameData.map[gameData.currentLevel][i][j]);
				b[i][j].setIcon(new ImageIcon(scaledImage));
			}
		}
	}
	private static Image chooseImage(int index){
		ImageIcon[] icons = new ImageIcon[10];
		Image scaledImage;
		icons[0]= new ImageIcon("Wall.jpg");
		icons[1]= new ImageIcon("Floor.jpg");
		icons[2]= new ImageIcon("Key.jpg");
		icons[3]= new ImageIcon("Door.jpg");
		icons[4]= new ImageIcon("Stair.jpg");
		icons[5]= new ImageIcon("Exit.jpg");
		icons[6]= new ImageIcon("Hero.jpg");
		icons[7]= new ImageIcon("Potion.jpg");
		icons[8]= new ImageIcon("Monster.jpg");
		if(index>10)
			scaledImage = icons[7].getImage().getScaledInstance(100, 100, Image.SCALE_SMOOTH);
		else if(index<0)
			scaledImage = icons[8].getImage().getScaledInstance(100, 100, Image.SCALE_SMOOTH);
		else
			scaledImage = icons[index].getImage().getScaledInstance(100, 100, Image.SCALE_SMOOTH);
		return scaledImage;
	}
}
